package com.force.abril.booking.download;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import br.com.abril.booking.BookingService;
import br.com.abril.prevision.ExecutivePrevisionService;

import com.force.abril.booking.download.transformer.BookingTransformer;
import com.force.abril.executive.prevision.download.transformer.ExecutivePrevisionTransformer;
import com.force.provider.SalesForceServices;
import com.sforce.soap.partner.QueryResult;
import com.sforce.ws.ConnectionException;

@Service
public class BookingServiceActivator implements BookingSelector {

	@Autowired
	private BookingService service;
	
	@Autowired
	private SalesForceServices salesforceService;
	
	@Autowired
	private BookingTransformer transformer;
	
	public BookingServiceActivator() {
	}

    public void downloadBookings () throws ConnectionException {
    	QueryResult result = salesforceService.query(SELECT);
    	boolean hasMoreRecords = false;
    	while (!hasMoreRecords) {
    		
    		service.save( transformer.toBookingsFromSObject(result.getRecords()));
    		if (!result.getDone()) {
    			result = salesforceService.queryMore(result.getQueryLocator());
    			continue;
    		}
    		
    		hasMoreRecords = result.getDone();
    	}
     	
     	
    }
	
}
